// 简单的内存 + localStorage 存储，仅字段 id、name
const KEY = 'teams.simple.list';

function read() {
  try {
    const raw = localStorage.getItem(KEY);
    if (!raw) return [];
    const arr = JSON.parse(raw);
    if (Array.isArray(arr)) return arr;
    return [];
  } catch (e) {
    return [];
  }
}

function write(list) {
  localStorage.setItem(KEY, JSON.stringify(list));
}

export function getTeams() {
  const list = read();
  if (list.length === 0) {
    const seed = [
      { id: '1', name: 'Team A' },
      { id: '2', name: 'Team B' },
      { id: '3', name: 'Team C' },
    ];
    write(seed);
    return seed;
  }
  return list;
}

export function getTeamById(id) {
  const list = read();
  return list.find(t => String(t.id) === String(id));
}

export function saveTeam(team) {
  const list = read();
  const idx = list.findIndex(t => String(t.id) === String(team.id));
  if (idx >= 0) {
    list[idx] = { id: String(team.id), name: team.name };
  } else {
    list.push({ id: String(team.id), name: team.name });
  }
  write(list);
}


